<?php 
//INSERT INTO uang_muka(order_no,date_dp,date_lunas,isLunas,amount)VALUES()

include_once($path_to_root . "/includes/date_functions.inc");

function add_dp($orderno,$amount)
{
	$sql = "INSERT INTO uang_muka(order_no,date_dp,isLunas,pay1) VALUES("
	.db_escape($orderno) .", '"
	.date2sql(Today())."', 0, ".db_escape($amount)." )";
	db_query($sql,"The uang muka could not be added");	
}


function get_sales_order($order_no)
{
	$sql = "select total from ".TB_PREF."sales_orders where order_no = $order_no";
	$row = db_query($sql,"Total could error");
	return db_fetch($row);
}

function  get_uang_muka($order_no)
{
	$sql = "select pay1 from ".TB_PREF."uang_muka where order_no = $order_no";
	$row = db_query($sql,"uang muka error");
	return db_fetch($row);	
}

function update_lunas($orderno,$amount)
{
	$sql = "INSERT INTO uang_muka(order_no,date_dp,isLunas,pay1) VALUES("
	.db_escape($orderno) .", '"
	.date2sql(Today())."', 0, ".db_escape($amount)." )";
	db_query($sql,"The uang muka could not be added");	
}

function isPosibleFinish($isdp)
{
	if($isdp)
	$sql = "select order_no from ".TB_PREF."sales_orders WHERE reference != 'auto' and order_no not IN(select order_no from ".TB_PREF."uang_muka where !isLunas)";
	else
	$sql = "select order_no from uang_muka where !isLunas";
	
	$row = db_query($sql,"could not check this situation.");
	return mysql_num_rows($row) > 0;
}

function isAmountValid($orderno,$amount)
{
	$sql = "select (so.total - um.pay1) selisih from uang_muka um LEFT JOIN sales_orders so ON um.order_no = so.order_no where um.order_no = '".$orderno."'";
	$row = db_query($sql,"could not check this situation.");	
	return mysql_fetch_field($row) == 0;	
}

?>